package nc.vo.so.m4331.rule;

import java.util.HashSet;
import java.util.Set;
import nc.vo.pubapp.margin.BillMarginContext;
import nc.vo.pubapp.margin.MarginContextFactory;
import nc.vo.pubapp.margin.MarginEntry;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.margin.SCMMuiltyWordMarginJudgement;
import nc.vo.scmpub.res.billtype.SOBillType;
import nc.vo.scmpub.res.billtype.TOBillType;
import nc.vo.so.m4331.entity.DeliveryVO;












public class DeliveryMarginRule
{
  private final String[] destMnyForOrder = { 
      "norigmny", "norigtaxmny", "norigdiscount", "nmny", "ntaxmny", "ntax", "ndiscount", "ngroupmny", "ngrouptaxmny", "nglobalmny", "nglobaltaxmny" };





  
  private final String[] destMnyForTran = { "norigmny", "norigtaxmny", "nmny", "ntaxmny", "ntax" };


  
  private final String[] srcorderMNY = { 
      "norigmny", "norigtaxmny", "norigdiscount", "nmny", "ntaxmny", "ntax", "ndiscount", "ngroupmny", "ngrouptaxmny", "nglobalmny", "nglobaltaxmny" };






  
  private final String[] srctranMNY = { "norigmny", "norigtaxmny", "nmny", "ntaxmny", "ntax" };



  
  public void process(DeliveryVO[] vos) {
    try {
      if (vos == null) {
        return;
      }
      Set<DeliveryVO> orderSet = new HashSet<DeliveryVO>();
      Set<DeliveryVO> tranSet = new HashSet<DeliveryVO>();
      for (DeliveryVO vo : vos) {
        if (vo.getChildrenVO() == null || vo.getChildrenVO().length == 0) {
          return;
        }
        
        String billtype = vo.getChildrenVO()[0].getVsrctype();
        
        if (SOBillType.Order.getCode().equals(billtype)) {
          orderSet.add(vo);
        }
        if (TOBillType.TransOrder.getCode().equals(billtype)) {
          tranSet.add(vo);
        } 
      } 
      if (orderSet.size() > 0) {
        SCMMuiltyWordMarginJudgement sCMMuiltyWordMarginJudgement = new SCMMuiltyWordMarginJudgement(new String[] { "norignetprice", "norigtaxnetprice" }, new String[] { "norignetprice", "norigtaxnetprice" });

        
        DeliveryVO[] newvos = new DeliveryVO[orderSet.size()];
        newvos = (DeliveryVO[])orderSet.toArray(newvos);
        
        BillMarginContext context = MarginContextFactory.getInstance().produceMarginContext(newvos, SOBillType.Order
            .getCode(), "nnum", this.srcorderMNY, SOBillType.Delivery
            .getCode(), "nnum", this.destMnyForOrder, "csrcbid", "csrcid", sCMMuiltyWordMarginJudgement);

        
        MarginEntry.getInstance().process(context);
      } 
      if (tranSet.size() > 0) {
        SCMMuiltyWordMarginJudgement sCMMuiltyWordMarginJudgement = new SCMMuiltyWordMarginJudgement(new String[] { "norignetprice", "norigtaxnetprice" }, new String[] { "norignetprice", "norigtaxnetprice" });

        
        DeliveryVO[] newvos = new DeliveryVO[tranSet.size()];
        newvos = (DeliveryVO[])tranSet.toArray(newvos);
        
        BillMarginContext context = MarginContextFactory.getInstance().produceMarginContext(newvos, TOBillType.TransOrder
            .getCode(), "nnum", this.srctranMNY, SOBillType.Delivery
            .getCode(), "nnum", this.destMnyForTran, "csrcbid", "csrcid", sCMMuiltyWordMarginJudgement);

        
        MarginEntry.getInstance().process(context);
      }
    
    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
  }
}
